/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import control.MStockMidlet;
import javax.microedition.lcdui.Display;
import listener.Command;
import listener.ICommandListener;
import listener.IKeyListener;
import model.ErrorMessage;
import ui.AGridItem;
import ui.Grid;
import ui.GridItem;

/**
 *
 * @author tho.hoang
 */
public class GridForm extends AForm implements ICommandListener, IKeyListener {

    private Grid grid;

    public GridForm() {
    }

    public GridForm(MStockMidlet control, Display display) {
        super(control, display);
    }

    protected void initData() {
        this.lang = new String[]{"Chọn",
                    "Thoát",
                    "Xổ số 3 miền",
                    "Vé số chiều sổ",
                    "Dò số nhanh",
                    "Kết quả theo ngày",
                    "Kết quả theo đài",
                    "Thống kê - Phân tích",
                    "Lịch mở thưởng",
                    "Gửi đến bạn bè",
                    "Tùy chọn",
                    "Hướng dẫn",
                    "Giới thiệu"};

        this.recycle = control.getRecycle();
    }

    protected void initGui() {
        this.ok = new Command(lang[0], Command.OK);
        this.back = new Command(lang[1], Command.EXIT);
        this.grid = new Grid(display, control, lang[2], background, icon);

        grid.setSelectCommand(ok);
        grid.setCancelCommand(back);
        grid.setCommandListener(this);
        grid.setKeyListener(this);

        grid.setColumns(4);

        grid.addItem(new GridItem("1", control.getImages()[0], "Menu 1"));
        grid.addItem(new GridItem("2", control.getImages()[1], "Menu 2"));
        grid.addItem(new GridItem("3", control.getImages()[2], "Menu 3"));
        grid.addItem(new GridItem("4", control.getImages()[3], "Menu 4"));
        grid.addItem(new GridItem("5", control.getImages()[4], "Menu 5"));
        grid.addItem(new GridItem("6", control.getImages()[5], "Menu 6"));
        grid.addItem(new GridItem("7", control.getImages()[6], "Menu 7"));
        grid.addItem(new GridItem("8", control.getImages()[7], "Menu 8"));
        grid.addItem(new GridItem("9", control.getImages()[8], "Menu 9"));
        grid.addItem(new GridItem("10", control.getImages()[0], "Menu 10"));
        grid.addItem(new GridItem("11", control.getImages()[0], "Menu 11"));
        grid.addItem(new GridItem("12", control.getImages()[0], "Menu 12"));
        grid.addItem(new GridItem("13", control.getImages()[0], "Menu 13"));
        grid.addItem(new GridItem("14", control.getImages()[0], "Menu 14"));
        grid.addItem(new GridItem("15", control.getImages()[0], "Menu 15"));
        grid.addItem(new GridItem("16", control.getImages()[0], "Menu 16"));
        grid.addItem(new GridItem("17", control.getImages()[1], "Menu 17"));
        grid.addItem(new GridItem("18", control.getImages()[2], "Menu 18"));

        grid.setSelectedIndex(6);

        grid.show();
    }

    protected void loadData() {
    }

    protected void showData() {
    }

    protected void showError(ErrorMessage error) {
    }

    public void commandAction(Command command) {
        int i = grid.getSelectedIndex();
        if (command == ok) {
            AGridItem item = grid.getItemAt(i + 1);
            grid.setSelectedItem(item);
        } else {
            AGridItem item = grid.getItemAt(i - 1);
            grid.setSelectedItem(item);
        }
        grid.repaint();
    }

    public void keyAction(int key) {
        if (key == Grid.FIRE) {
            commandAction(ok);
        }
    }
}
